package com.apesource.mallproject.controller;

import com.apesource.mallproject.pojo.Brand;
import com.apesource.mallproject.service.IBrandService;
import com.apesource.mallproject.utils.ChineseCharacterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin(allowCredentials = "true", maxAge = 3600)
public class BrandController {
    @Autowired
    private IBrandService brandService;


    @RequestMapping("/findBrand")
    public List<Brand> findBrand() {
        return brandService.queryAllBrand();
    }

    //根据品牌获取首字母
    @RequestMapping("/first/{name}")
    public String getFirstCharc(@PathVariable String name) {
        return ChineseCharacterUtil.convertCharc(name);
    }

    //新增品牌
    @RequestMapping(value = "/addBrand")
    public void insertBrand(@RequestParam("name") String name, @RequestParam("jPing") String jPing) {
        System.out.println(name);
        brandService.insertBrand(name, jPing);
    }

    //修改
    @RequestMapping(value = "/updateBrand")
    public void updateBrand(@RequestParam("name") String name, @RequestParam("jPing") String jPing, @RequestParam("id") Integer id) {
        System.out.println(name);
        brandService.updateBrand(name, jPing, id);
    }

    //删除一个
    @RequestMapping("/delBrand/{id}")
    public void delBrand(@PathVariable Integer id) {
        brandService.delBrand(id);
    }

    //批量删除
    @RequestMapping("/batchDel")
    public void batchDel(@RequestBody HashMap<String, String> idsMap) {
        String id = idsMap.get("ids");
        System.out.println(id);
        String[] ids = id.split(",");
        System.out.println(Arrays.toString(ids));
        brandService.batchDelete(ids);
    }


}
